home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / lanutsrc.zip / LOGIN.C < prev    next >
Text File  |  1992-07-14  |  14KB  |  510 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. #include <direct.h>
  6. #include <conio.h>
  7. #include <process.h>
  8.  
  9. #include "lantasti.h"
  10.  
  11. #define DOS 0x21
  12. #define NETBIOS 0x5C
  13. #define DELIM "+, "
  14.  
  15. typedef struct FLAGS {
  16.         int script,
  17.             suppress,
  18.             duplicates,
  19.             stop;
  20.             char drive[3];
  21.          } FLAGS;
  22.          
  23. #define LIST_SIZE 30
  24. #define NAME_SIZE 20
  25. typedef struct LIST {
  26.          int  num_servers,num_ids,num_pw;
  27.         char server[LIST_SIZE][NAME_SIZE],
  28.              user[LIST_SIZE][NAME_SIZE],
  29.              password[LIST_SIZE][NAME_SIZE];
  30.            } LIST;    
  31.            
  32. /* definitions to save typing time */
  33. #define C_SERVER list->server[i % list->num_servers]
  34. #define C_USER   list->user[i % list->num_ids]
  35. #define C_PASS   list->password[i % list->num_pw]
  36.  
  37.   FLAGS flags = {
  38.    TRUE,
  39.    FALSE,
  40.    TRUE,
  41.    FALSE,
  42.    "B:"
  43.  };
  44.   int ret_code = 0;
  45.   
  46. /* select_drive *************************************************************
  47.   Select current disk drive given drive letter. Returns number of drives
  48.   on system.
  49. ****************************************************************************/
  50. int select_drive(drive)
  51.   char drive;
  52. {
  53.   union REGS regs;
  54.   
  55.   drive = drive - 65;          /* convert to drive number */
  56.   regs.h.ah = 14;              /* select drive function */
  57.   regs.h.dl = drive;
  58.   intdos(®s,®s);
  59.   return(regs.h.al);           /* number of drives on system */
  60. }
  61.  
  62. /* get_drive ********************************************************************
  63.  Returns the letter of the current disk drive
  64. *****************************************************************************/
  65. char get_drive() {
  66.   union REGS regs;
  67.   
  68.   regs.h.ah = 0x19;
  69.   intdos(®s,®s);       /* get drive number */
  70.   
  71.   return(regs.h.al + 65);    /* convert to drive letter and return */
  72. }
  73.  
  74. /* fexists ********************************************************************
  75.  
  76. *****************************************************************************/
  77. int fexists(filename)
  78.   char *filename;
  79. {
  80.   FILE *file;
  81.   int result;
  82.   
  83.   file = fopen(filename,"rb");
  84.   result = (file != NULL);
  85.   if (result) fclose(file);
  86.   return(result);
  87.   
  88. }
  89.  
  90. /* pwgets ********************************************************************
  91.  
  92. *****************************************************************************/
  93. char *pwgets(buffer,length)
  94.   char *buffer;
  95.   int length;
  96. {
  97.   int i,key;
  98.  
  99.   i = 0;
  100.   while (i < length) {
  101.     key = getch();
  102.     if (key == '\r') {
  103.       putch(key);
  104.       putch('\n');
  105.       break;
  106.     }
  107.     buffer[i] = key;
  108.     if (key == '\b') {
  109.       if (i > 0) {
  110.         printf("\b \b");
  111.         i--;
  112.       }
  113.       else putch('\a');
  114.     }
  115.     else {
  116.       putch('∙');
  117.       i++;
  118.     }
  119.   }  
  120.         
  121.   buffer[i] = 0;  
  122.   return(buffer);
  123. }
  124.  
  125. /* getstring ********************************************************************
  126.  Get a string from the console -- takes a pointer to a string buffer, the 
  127.  longest permissible length and two switches. The empty switch determines
  128.  whether or not the user is allowed to enter an empty string. If TRUE, he
  129.  can, if FALSE, he must enter something.  The shown switch determines 
  130.  whether or not input is echoed to the screen, TRUE if echoed, FALSE if
  131.  invisible
  132. *****************************************************************************/
  133. void getstring(prompt,buffer,length,empty,shown) 
  134.   char *prompt,*buffer;
  135.   int length,empty,shown;
  136. {
  137.   int i;
  138.   
  139.   if (shown) {
  140.     fprintf(stdout,"%s",prompt);
  141.     fgets(buffer,length,stdin);
  142.     if (buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = 0;
  143.     while (!(strlen(buffer) || empty)) {
  144.       fprintf(stdout,"%s",prompt);
  145.       fgets(buffer,length,stdin);
  146.       if (buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = 0;
  147.     }
  148.   }
  149.   else {   /* get string without echoing */
  150.   
  151.     fprintf(stdout,"%s",prompt);
  152.     pwgets(buffer,length);
  153.     
  154.     while (!(strlen(buffer) || empty)) {
  155.       fprintf(stdout,"%s",prompt);
  156.       pwgets(buffer,length);
  157.     }
  158.   }
  159. }
  160.  
  161. /* error ********************************************************************
  162.  
  163. *****************************************************************************/
  164. void error(code,message)
  165.   int code;
  166.   char *message;
  167. {
  168.   char *ptr;
  169.   
  170.   ret_code = (code) ? code : -1;
  171.   if (flags.suppress) return;
  172.   
  173.   if (code) {
  174.     ptr = get_error_text(code);
  175.     puts(ptr);
  176.   }
  177.   else puts(message);
  178. }
  179.  
  180. /* get_available_servers ********************************************************************
  181.  
  182. *****************************************************************************/
  183. void get_available_servers(list)
  184.   LIST *list;
  185. {
  186.   char buffer[17];
  187.   int index,result;
  188.   
  189.   index = 0;
  190.   result = get_inactive_server(buffer,index);
  191.   while (result) {
  192.     strcpy(list->server[list->num_servers++],buffer);
  193.     if (list->num_servers >= LIST_SIZE) {
  194.       puts("Warning: Too many servers.  Only the first 50 can be used.");
  195.       break;
  196.     }
  197.     result = get_inactive_server(buffer,++index);
  198.   }
  199. }
  200.  
  201. /* process_server_list ********************************************************************
  202.  
  203. *****************************************************************************/
  204. void process_server_list(string,list)
  205.   char *string;
  206.   LIST *list;
  207. {
  208.   char *ptr;
  209.   
  210.   ptr = strtok(string,DELIM);
  211.   
  212.   while (ptr !=NULL) {
  213.     if (*ptr == '*') {
  214.       get_available_servers(list);
  215.       break;
  216.     }
  217.     else {
  218.       strcpy(list->server[list->num_servers++],ptr);
  219.     }
  220.  
  221.     if (list->num_servers >= LIST_SIZE ) {
  222.       puts("Warning: Too many servers.  Only the first 30 will be used.");
  223.       break;
  224.     }
  225.     ptr = strtok(NULL," ,");
  226.   }
  227.  
  228. /* if no servers are given, ask for one */
  229.   if (list->num_servers == 0) {
  230.     strcpy(list->server[list->num_servers++],"?");
  231.   }
  232. }
  233.   
  234. /* process_user_list *********************************************************
  235.  
  236. *****************************************************************************/
  237. void process_user_list(string,list)
  238.   char *string;
  239.   LIST *list;
  240. {
  241.   char *ptr;
  242.   
  243.   ptr = strtok(string,DELIM);
  244.   
  245.   while (ptr != NULL) {
  246.     strcpy(list->user[list->num_ids++],ptr);
  247.     if (list->num_ids >= LIST_SIZE ) {
  248.       puts("Warning: Too many user IDs.  Only the first 30 can be used.");
  249.       break;
  250.     }
  251.     ptr = strtok(NULL,DELIM);
  252.   }
  253.  
  254. /* if no user ids are given, ask for one */
  255.   if (list->num_ids == 0) {
  256.     strcpy(list->user[list->num_ids++],"?");
  257.   }
  258. }
  259.   
  260. /* process_password_list *****************************************************
  261.  
  262. *****************************************************************************/
  263. void process_password_list(string,list)
  264.   char *string;
  265.   LIST *list;
  266. {
  267.   char *ptr;
  268.   
  269.   ptr = strtok(string,DELIM);
  270.   
  271.   while (ptr !=NULL) {
  272.     strcpy(list->password[list->num_pw++],ptr);
  273.     if (list->num_pw >= LIST_SIZE ) {
  274.       puts("Warning: Too many passwords.  Only the first 30 can be used.");
  275.       break;
  276.     }
  277.     ptr = strtok(NULL," ,");
  278.   }
  279.  
  280. /* if no passwords given, set up a zero length string */
  281.   if (list->num_pw == 0) {
  282.     list->password[list->num_pw++][0] = 0;  
  283.   }
  284. }
  285.  
  286. /* options */
  287. #define NUM_OPTIONS 4
  288. char *options[NUM_OPTIONS] = {
  289.     "NODUPLICATES",
  290.     "NOSCRIPT",
  291.     "SUPPRESS",
  292.     "HELP"
  293.   };
  294.  
  295. /* process_option ********************************************************************
  296.  
  297. *****************************************************************************/
  298. void process_option(string,flags)
  299.   char *string;
  300.   FLAGS *flags;
  301. {
  302.   char *ptr;
  303.   int i; 
  304.   
  305.   ptr = strtok(string,"=:");
  306.   
  307.   for (i = 0; i < NUM_OPTIONS; i++) {
  308.     if (!strcmpi(ptr,options[i])) break;
  309.   }
  310.   
  311.   if (i >= NUM_OPTIONS) {
  312.     printf("Whoops! %s isn't a valid command line option.\n",ptr);  
  313.     return;
  314.   }
  315.  
  316.   ptr = strtok(NULL," =");
  317.   
  318.   switch (i)  {
  319.     case 0:   /* report duplicate logins */
  320.       flags->duplicates = FALSE;
  321.       break;
  322.     case 1:   /* no script */
  323.       flags->script = FALSE;
  324.       break;
  325.     case 2:   /* suppress error messages */
  326.       flags->suppress = TRUE;
  327.       break;
  328.     case 3:   /* help */
  329.       puts("LOGIN utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
  330.       puts("All rights reserved.  LANtastic is a trademark of Artisoft, Inc.\n");
  331.       puts("Usage: LOGIN <server list> <user id list> <password list> [/OPTIONS]");
  332.       puts("  The '*' character in the server lists means all available servers.");
  333.       puts("  A question mark in any list causes the program to prompt the user");
  334.       puts("  for input (passwords will not be displayed).\n");
  335.       puts("Available options are:");
  336.       puts("/NODUPLICATES - report duplicate login attempts");
  337.       puts("/NOSCRIPT     - bypass the login script facility");
  338.       puts("/SUPPRESS     - suppress all error messages");
  339.       puts("/HELP         - display this documentation");
  340.       exit(0);
  341.       break;
  342.   }
  343. }
  344.  
  345. /* scan_command_line ********************************************************************
  346.  
  347. *****************************************************************************/
  348. scan_command_line(argc,argv,flags,list)
  349.   int argc;
  350.   char *argv[];
  351.   FLAGS *flags;
  352.   LIST *list;
  353. {
  354.   int i,state;
  355.   char s_buffer[128],u_buffer[128],p_buffer[128];
  356.   
  357.   s_buffer[0] = u_buffer[0] = p_buffer[0] = 0;
  358.   state = list->num_servers = list->num_ids = list->num_pw = 0;
  359.   
  360.   for (i = 1;i < argc; i++) {
  361.     strupr(argv[i]);
  362.     if (argv[i][0] == '/') {
  363.       process_option(&argv[i][1],flags);
  364.       continue;
  365.     }
  366.     switch (state++) {
  367.       case 0:
  368.         strcpy(s_buffer,argv[i]);
  369.         break;
  370.       case 1:
  371.         strcpy(u_buffer,argv[i]);
  372.         break;
  373.       case 2:
  374.         strcpy(p_buffer,argv[i]);
  375.         break;
  376.     }
  377.   }
  378. /* call the setup functions */
  379. process_server_list(s_buffer,list);
  380. process_user_list(u_buffer,list);
  381. process_password_list(p_buffer,list);
  382.  
  383. }
  384.  
  385. /* do_script ********************************************************************
  386.  
  387. *****************************************************************************/
  388. do_script(server,user,drive)
  389.   char *server,*user,*drive;
  390. {
  391.   char string[60],node[17],starting_drive;
  392.   int result;
  393.  
  394. /* get our node name */
  395.    whoami(node);
  396.  
  397. /* save the current drivespec */   
  398.    starting_drive = get_drive();
  399.  
  400. /* redirect the server's network directory */  
  401.   sprintf(string,"\\\\%s",server);
  402.   result = redirect_device(drive,string,DISK_DEVICE);
  403.   if (!result) {
  404.   
  405. /* execute the system login script */  
  406.     select_drive(flags.drive[0]);
  407.     sprintf(string,"%s$SYSTEM.BAT",drive);
  408.     if (fexists(string)) {
  409.       sprintf(string,"%s %s",string,node);
  410.       system(string);
  411.     }
  412.   
  413. /* execute the user's login script */
  414.     sprintf(string,"%s%s.BAT",drive,user);
  415.     if (fexists(string)) {
  416.       sprintf(string,"%s %s",string,node);
  417.       system(string);
  418.     }
  419.   
  420. /* restore the script drive to normal */  
  421.     if (get_drive() == flags.drive[0]) select_drive(starting_drive);
  422.     cancel_redirection(flags.drive);
  423.   }
  424.   else {
  425.     sprintf(string,"Unable to access network directory on server %s\n",server);
  426.     error(FALSE,string);
  427.   }
  428. }
  429.  
  430. /* do_login ********************************************************************
  431.  
  432. *****************************************************************************/
  433. int do_login(flags,list)
  434.   FLAGS *flags;
  435.   LIST *list;
  436. {
  437.   int result,i,interactive;
  438.   char login_buffer[60],
  439.        logout_buffer[60],
  440.        prompt[50],
  441.        *ptr;
  442.   
  443. /* build \\SERVER<0>USERID<0>PASSWORD<0><0> string */
  444.  
  445. /* first, initialize the whole login buffer to zeros to make sure
  446.    that we don't get tripped up because LANtastic needs an extra
  447.    terminating 0 if there's no password */
  448.     memset(login_buffer,0,60);
  449.  
  450.   for (i = 0; i < list->num_servers; i++) {
  451. /* see if we've got any "?"s in the list */
  452.     if (C_SERVER[0] == '?') {
  453.       getstring("Server: ",login_buffer,128,FALSE,TRUE);
  454.       list->num_servers = i;
  455.       process_server_list(login_buffer,list);
  456.     }
  457.  
  458.     if (C_USER[0] == '?') {
  459.       if (list->num_ids > 1) 
  460.         sprintf(prompt,"User Name (for server %s): ",C_SERVER);
  461.       else sprintf(prompt,"User Name: ");
  462.       getstring(prompt,C_USER,128,FALSE,TRUE);
  463.       if (C_PASS[0] == 0) strcpy(C_PASS,"?");
  464.     }
  465.     
  466.     if (C_PASS[0] == '?') {
  467.       if (list->num_pw > 1) 
  468.         sprintf(prompt,"Password (for server %s): ",C_SERVER);
  469.       else sprintf(prompt,"Password: ");
  470.         getstring(prompt,C_PASS,128,TRUE,FALSE);
  471.     }
  472.     
  473.     sprintf(login_buffer,"\\\\%s\\%s",C_SERVER,C_USER);
  474.     ptr = login_buffer + strlen(login_buffer) + 1;
  475.     strcpy(ptr,C_PASS);
  476.     sprintf(logout_buffer,"\\\\%s",C_SERVER);
  477.  
  478.     logout(logout_buffer);
  479.     result = login(login_buffer);
  480.  
  481.     if (result == 0) {
  482.       if (flags->script) do_script(C_SERVER,C_USER,flags->drive);
  483.     }
  484.     else error(result,NULL);
  485.   }
  486. }
  487.  
  488. /* main ********************************************************************
  489.  
  490. *****************************************************************************/
  491. int main(argc,argv)
  492.   int argc;
  493.   char *argv[];
  494. {
  495.   LIST  list;             /*server, user and password lists*/    
  496.   int serverno;             /*index into serverlist*/
  497.  
  498. #ifdef SHAREWARE
  499.   puts("LOGIN utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
  500.   puts("All rights reserved.  Thanks for trying this unregistered Shareware edition!\n");
  501. #endif  
  502.   
  503.   scan_command_line(argc,argv,&flags,&list);  
  504.   
  505.   if (list.num_servers > 0) do_login(&flags,&list);
  506.   else error(FALSE,"No available servers found -- Unable to log in.");
  507.  
  508.   return(ret_code);
  509. }
  510.